<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> 维保合同 </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'maintenance:management:inContract:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ tid: record.tid })">
          {{ record.name }}
        </a>
      </template>
      <!-- 新增 action 插槽 -->
      <template #action="{ record }">
        <a-button @click="handleForm({ tid: record.tid })" v-auth="'maintenance:management:inContract:edit'" type="text">
          <span style="color: #52c41a;font-size:14px">编辑</span>
        </a-button>
        <a-button @click="handleFormDetail({ tid: record.tid })" type="text">
          <span style="font-size: 14px; color: #4169E1;">详情</span>
        </a-button>
        <!-- 
        <a-button @click="handleFormTask({ tid: record.tid })" v-auth="'maintenance:management:inContract:edit'" type="text" v-if="record.state === '过期'">
          <span style="font-size: 12px; color: #4169E1;">生成维保任务</span>
        </a-button>
        -->
        <Popconfirm
          title="是否确认删除维保合同表"
          ok-text="确认"
          cancel-text="取消"
          @confirm="handleDelete(record)"
          @cancel="handleCancel"
        >
          <a-button 
            v-auth="'maintenance:management:inContract:edit'" 
            type="text" 
            style="color: red;"
          >
            <span style="font-size: 14px;">删除</span>
          </a-button>
        </Popconfirm>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <!-- <TaskForm @register="registerTaskFormDrawer" @success="handleSuccess" /> -->
    <FormDetail @register="registerFormDetailDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsMaintenanceManagementInContractList">
  import { unref,ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { inContractDelete, inContractListData } from '/@/api/maintenance.management/inContract';
  import { unitEnterpriseDominantListData } from '/@/api/unitmanage/unitEnterpriseDominant';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import TaskForm from './task.vue';
  import FormDetail from './formDetail.vue';
  import { officeTreeData } from '/@/api/sys/office';
  import { Popconfirm } from 'ant-design-vue'; // 引入 Popconfirm 组件

  const { t } = useI18n('maintenance.management.inContract');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('维保合同表管理'),
  };

  const EnterpriseParams = ref<Recordable>({
    name:''
  });
  const inContractParams = ref<Recordable>({
    name:''
  });  

  const maintenanceEnterpriseParams = ref<Recordable>({
    name:'',
    dominantType:'maintenance'
  });

  const searchForm: FormProps = {
    baseColProps: { md: 8, lg: 6 },
    labelWidth: 90,
    schemas: [
      {
        label: t(''),
        field: 'id',
        fieldLabel: 'name',
        component: 'Select',
        componentProps: () => {
          return {
            placeholder: t('合同名称'),
            api: inContractListData,
            params: inContractParams.value,
            fieldNames: { label: 'name', value: 'id' },
            resultField: 'list',
            notFoundContent: '未查询到记录',
            filterTreeNode: false,
            allowClear: true,
            filterOption: false,
            immediate: true,
            onSearch: (value: string) => {
              inContractParams.value.name = value;
              console.log(inContractParams.value);
            },
          };
        },
      },
      {
        label: t(''),
        field: 'aenterpriseId',
        fieldLabel:'aenterpriseName',
        component: 'Select',
        componentProps: () => {
          return {
            placeholder: t('请输入业主企业'),
            api: unitEnterpriseDominantListData,
            params: EnterpriseParams.value,
            fieldNames: { label: 'name', value: 'unitCode' },
            resultField: 'list',
            notFoundContent: '未查询到记录',
            filterTreeNode: false,
            allowClear: true,
            filterOption: false,
            immediate: true,
            onSearch: (value: string) => {
              EnterpriseParams.value.name = value;
              console.log(EnterpriseParams.value);
            },
          };
        },
      },
      {
        label: t(''),
        field: 'benterpriseId',
        fieldLabel:'benterpriseName',
        component: 'Select',
        componentProps: () => {
          return {
            placeholder: t('请输入维保企业'),
            api: unitEnterpriseDominantListData,
            params: maintenanceEnterpriseParams.value,
            fieldNames: { label: 'name', value: 'unitCode' },
            resultField: 'list',
            notFoundContent: '未查询到记录',
            filterTreeNode: false,
            allowClear: true,
            filterOption: false,
            immediate: true,
            onSearch: (value: string) => {
              maintenanceEnterpriseParams.value.name = value;
              console.log(maintenanceEnterpriseParams.value);
            },
          };
        },
      },
      {
        label: t(''),
        field: 'state',
        component: 'Input',
        componentProps:{
          placeholder: t('请输入合同状态')
        }
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('合同名称'),
      dataIndex: 'name',
      key: 'a.name',
      sorter: true,
      width: 230,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('业主单位'),
      dataIndex: 'aenterpriseName',
      key: 'a.a_enterprise_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('维保单位'),
      dataIndex: 'benterpriseName',
      key: 'a.b_enterprise_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('合同起始年月'),
      dataIndex: 'startDate',
      key: 'a.start_date',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('合同结束年月'),
      dataIndex: 'endDate',
      key: 'a.end_date',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('合同状态'),
      dataIndex: 'state',
      key: 'a.state',
      sorter: true,
      width: 130,
      align: 'left',
      dictType: 'contract_state',
    },
    {
      title: t('操作'),
      dataIndex: 'action',
      key: 'action',
      width: 160,
      fixed: 'right',
      slot: 'action'
    }
  ];

  const [registerTable, { reload }] = useTable({
    api: inContractListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: null, // 移除原 actionColumn
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  const [registerDrawer, { openDrawer }] = useDrawer();
  // const [registerTaskFormDrawer, { openDrawer: openTaskFormDrawer }] = useDrawer();
  const [registerFormDetailDrawer, { openDrawer: openFormDetailDrawer }] = useDrawer();

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }
  // function handleFormTask(record: Recordable) {
  //   openTaskFormDrawer(true, record);
  // }
  function handleFormDetail(record: Recordable) {
    openFormDetailDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const params = { tid: record.tid };
    const res = await inContractDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }

  function handleCancel() {
    console.log('取消删除操作');
  }
</script>    